package day19.StringClass;

public class StringTest {
    public static void main(String[] args) {
        // 测试String的基本功能
        
        // 1. 字符串创建
        String str1 = "Hello";
        String str2 = new String("World");
        String str3 = "Hello"; // 与str1相同内容
        
        System.out.println("=== 字符串创建 ===");
        System.out.println("str1: " + str1);
        System.out.println("str2: " + str2);
        System.out.println("str3: " + str3);
        
        // 2. 字符串比较
        System.out.println("\n=== 字符串比较 ===");
        System.out.println("str1 == str3: " + (str1 == str3)); // true，因为都在字符串常量池中
        System.out.println("str1.equals(str3): " + str1.equals(str3)); // true，内容相同
        System.out.println("str1 == str2: " + (str1 == str2)); // false，str2是新创建的对象
        System.out.println("str1.equals(str2): " + str1.equals(str2)); // false，内容不同
        
        // 3. 字符串常用方法
        System.out.println("\n=== 字符串常用方法 ===");
        String testStr = "Hello Java World";
        System.out.println("原字符串: " + testStr);
        System.out.println("字符串长度: " + testStr.length());
        System.out.println("第7个字符: " + testStr.charAt(6));
        System.out.println("World的索引位置: " + testStr.indexOf("World"));
        System.out.println("最后一个l的位置: " + testStr.lastIndexOf('l'));
        
        // 4. 字符串截取和分割
        System.out.println("\n=== 字符串截取和分割 ===");
        System.out.println("substring(6): " + testStr.substring(6));
        System.out.println("substring(6, 10): " + testStr.substring(6, 10));
        
        String[] parts = testStr.split(" ");
        System.out.print("分割后的字符串数组: ");
        for (String part : parts) {
            System.out.print(part + " ");
        }
        System.out.println();
        
        // 5. 字符串转换
        System.out.println("\n=== 字符串转换 ===");
        System.out.println("转大写: " + testStr.toUpperCase());
        System.out.println("转小写: " + testStr.toLowerCase());
        System.out.println("去除空格: " + "  Hello World  ".trim());
        
        // 6. 字符串替换
        System.out.println("\n=== 字符串替换 ===");
        System.out.println("替换Java为Python: " + testStr.replace("Java", "Python"));
        System.out.println("替换所有l为L: " + testStr.replaceAll("l", "L"));
        
        // 7. 字符串连接
        System.out.println("\n=== 字符串连接 ===");
        System.out.println("使用+: " + str1 + " " + str2);
        System.out.println("使用concat: " + str1.concat(" ").concat(str2));
        
        // 8. 字符串包含关系
        System.out.println("\n=== 字符串包含关系 ===");
        System.out.println("是否包含Java: " + testStr.contains("Java"));
        System.out.println("是否以Hello开头: " + testStr.startsWith("Hello"));
        System.out.println("是否以World结尾: " + testStr.endsWith("World"));
        
        // 9. 字符串与其他类型的转换
        System.out.println("\n=== 字符串与其他类型转换 ===");
        int num = 123;
        String numStr = String.valueOf(num);
        System.out.println("数字转字符串: " + num + " -> " + numStr);
        String strNum = "456";
        int parsedNum = Integer.parseInt(strNum);
        System.out.println("字符串转数字: " + strNum + " -> " + parsedNum);
        
        // 10. StringBuilder测试
        System.out.println("\n=== StringBuilder测试 ===");
        StringBuilder sb = new StringBuilder();
        sb.append("Hello");
        sb.append(" ");
        sb.append("StringBuilder");
        System.out.println("StringBuilder内容: " + sb.toString());
        System.out.println("反转后: " + sb.reverse().toString());
    }
}